package some.member.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import some.member.dao.MemberDao;
import some.member.domain.MemberCommand;
import some.member.validator.MemberValidator;

@Controller
@RequestMapping("/modifyMember.do")
public class ModifyMemberController {
	@Autowired
	private MemberDao memberDao;

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView form(HttpSession session){
		
		String id = (String)session.getAttribute("id");
		MemberCommand memberCommand = memberDao.getMember(id);
		
		return new ModelAndView("modifyMember","memberCommand",memberCommand);
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String submit(MemberCommand memberCommand,BindingResult result){
		
		new MemberValidator().validate(memberCommand, result);
		
		if(result.hasErrors()){
			return "modifyMember";
		}
		
		memberDao.modifyMember(memberCommand);
		
		return "redirect:/myPage.do";
	}
}







